package cool.ale.errorMessage.threadPool;

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * 项目启动时做的一些异步的操作所用的线程池
 *
 * @author dujlc
 * @date 2022/12/4 16:37
 * @see
 * @since 1.0
 */
public class AleErrorMessageThreadPool {
    /**
     * 核心线程数
     */
    private static final int corePoolSize = 1;
    /**
     * 最大线程数
     */
    private static final int maxPoolSize = 1;
    /**
     * 多余线程最大空闲时间
     */
    private static final int keepAlive = 30;

    /**
     * 线程池缓冲队列
     * 如果不手动指定容量，默认为Integer.MAX_VALUE，也就是无界队列。
     */
    private static final BlockingQueue poolQueue = new LinkedBlockingQueue(8);

    /**
     * 定义线程池
     */
    private static volatile ThreadPoolExecutor poolExecutor;

    /**
     * 初始化线程池
     * @return 将初始化好的线程池放到ioc容器中
     */
    public static ThreadPoolExecutor getInstance(){
        if (poolExecutor == null){
            synchronized (AleErrorMessageThreadPool.class){
                if (poolExecutor == null){
                    poolExecutor = new ThreadPoolExecutor(corePoolSize,
                            maxPoolSize,
                            keepAlive,
                            TimeUnit.SECONDS,
                            poolQueue,
                            new ThreadPoolExecutor.DiscardOldestPolicy());
                }
            }
        }
        return poolExecutor;
    }
}
